package top.ochiamalu.aioj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.ochiamalu.aioj.common.ErrorCode;
import top.ochiamalu.aioj.constant.ConfigConstants;
import top.ochiamalu.aioj.domain.dto.config.UpdateCarouselDTO;
import top.ochiamalu.aioj.domain.dto.config.UpdateStudyPlanDTO;
import top.ochiamalu.aioj.domain.dto.config.UpdateToolbarSetDTO;
import top.ochiamalu.aioj.domain.entity.Config;
import top.ochiamalu.aioj.domain.vo.config.CarouselVO;
import top.ochiamalu.aioj.exception.BusinessException;
import top.ochiamalu.aioj.mapper.ConfigMapper;
import top.ochiamalu.aioj.service.ConfigService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 配置服务实施
 *
 * @author ochiamalu
 * @date 2025/02/16
 */
@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config>
        implements ConfigService {

    @Override
    public List<CarouselVO> getCarousel() {
        List<Config> configList = this.lambdaQuery().eq(Config::getType, ConfigConstants.CAROUSEL_TYPE).list();
        if (configList == null || configList.isEmpty()) {
            return Collections.emptyList();
        }

        List<CarouselVO> list = new ArrayList<>();
        for (int i = 0; i < configList.size(); i++) {
            CarouselVO carouselVO = new CarouselVO();
            carouselVO.setId(i);
            carouselVO.setImageUrl(configList.get(i).getValue());
            list.add(carouselVO);
        }
        return list;
    }

    @Override
    @Transactional
    public void updateCarousel(UpdateCarouselDTO updateCarouselDTO) {
        Long count = this.lambdaQuery().eq(Config::getType, ConfigConstants.CAROUSEL_TYPE).count();
        if (count > 0) {
            LambdaQueryWrapper<Config> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Config::getType, ConfigConstants.CAROUSEL_TYPE);
            boolean remove = this.remove(wrapper);
            if (!remove) {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除原轮播图失败");
            }
        }

        updateCarouselDTO.getImageList().forEach(image -> {
            Config config = new Config();
            config.setType(ConfigConstants.CAROUSEL_TYPE);
            config.setValue(image);
            boolean save = this.save(config);
            if (!save) {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR, "保存轮播图失败");
            }
        });
    }

    @Override
    public void updateStudyPlan(UpdateStudyPlanDTO updateStudyPlanDTO) {
        Long count = this.lambdaQuery().eq(Config::getType, ConfigConstants.STUDY_PLAN_TYPE).count();
        if (count > 0) {
            LambdaQueryWrapper<Config> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Config::getType, ConfigConstants.STUDY_PLAN_TYPE);
            boolean remove = this.remove(wrapper);
            if (!remove) {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除原学习计划失败");
            }
        }

        updateStudyPlanDTO.getIdList().forEach(id -> {
            Config config = new Config();
            config.setType(ConfigConstants.STUDY_PLAN_TYPE);
            config.setValue(id);
            boolean save = this.save(config);
            if (!save) {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR, "保存学习计划失败");
            }
        });
    }

    @Override
    public void updateToolbarSet(UpdateToolbarSetDTO updateToolbarSetDTO) {
        Long count = this.lambdaQuery().eq(Config::getType, ConfigConstants.TOOL_BAR_SET_TYPE).count();
        if (count > 0) {
            LambdaQueryWrapper<Config> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Config::getType, ConfigConstants.TOOL_BAR_SET_TYPE);
            boolean remove = this.remove(wrapper);
            if (!remove) {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除原工具栏题集失败");
            }
        }

        updateToolbarSetDTO.getIdList().forEach(id -> {
            Config config = new Config();
            config.setType(ConfigConstants.TOOL_BAR_SET_TYPE);
            config.setValue(id);
            boolean save = this.save(config);
            if (!save) {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR, "保存工具栏题集失败");
            }
        });
    }
}
